#import "Dao.h"

@implementation Dao
-(NSMutableArray*)findZoneList:(NSString*)cityId{
    NSString *sql = @"SELECT ZoneName,ZoneID FROM T_Zone  where  CityID=";
    sql=[sql stringByAppendingFormat:@"%@",cityId];
    
    
    
    sqlite3_stmt *statement;
    NSMutableArray* list=[[NSMutableArray alloc] init];
    if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            
            ZonePO* po=[[ZonePO alloc] init];
            
            char *name = (char *)sqlite3_column_text(statement, 0);
            
            NSString *nameStr = [[NSString alloc] initWithUTF8String:name];
            po.zoneName=nameStr;
            
            
            
            char *provinceID = (char *)sqlite3_column_text(statement, 1);
            NSString *provinceIDStr = [[NSString alloc] initWithUTF8String:provinceID];
            po.zoneID=provinceIDStr;
            [list addObject:po];
            
            
        }
        sqlite3_finalize(statement);
        NSLog(@"findZoneList=%@",list);
        return list;
    }else{
        NSLog(@"ERROR");
    }
    return list;
    
    
}


-(NSArray*)findCityList:(NSString*)provinceId{
    NSString *sql = @"SELECT CityName,CitySort FROM T_City  where  ProID=";
    sql=[sql stringByAppendingFormat:@"%@",provinceId];
    NSLog(@"city.sql=%@",sql);
    sqlite3_stmt *statement;
    NSMutableArray* list=[[NSMutableArray alloc] init];
    if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            
            CityPO* po=[[CityPO alloc] init];
            
            char *name = (char *)sqlite3_column_text(statement, 0);
            
            NSString *nameStr = [[NSString alloc] initWithUTF8String:name];
            po.cityName=nameStr;
            
            
            
            char *provinceID = (char *)sqlite3_column_text(statement, 1);
            NSString *provinceIDStr = [[NSString alloc] initWithUTF8String:provinceID];
            po.cityID=provinceIDStr;
            [list addObject:po];
            
            
        }
        sqlite3_finalize(statement);
    }else{
        NSLog(@"ERROR  CITY...");
    }
    return list;

    
}
- (NSMutableArray*)findProvinceList{
    
    NSString *sql = @"SELECT ProName,ProSort FROM T_Province";
    sqlite3_stmt *statement;
    NSMutableArray* list=[[NSMutableArray alloc] init];
    ProvincePO* tmp=[[ProvincePO alloc] init];
    tmp.provinceName=@"请选择..";
    tmp.provinceID=0;
    [list addObject:tmp];
    if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            
            ProvincePO* po=[[ProvincePO alloc] init];
            
            char *name = (char *)sqlite3_column_text(statement, 0);
            
            NSString *nameStr = [[NSString alloc] initWithUTF8String:name];
            
            po.provinceName=nameStr;
            
          
            
            
            char *provinceID = (char *)sqlite3_column_text(statement, 1);
            NSString *provinceIDStr = [[NSString alloc] initWithUTF8String:provinceID];
            po.provinceID=provinceIDStr;
            [list addObject:po];
           
            
            
        }
        sqlite3_finalize(statement);
        NSLog(@"list=%d",[list count]);
        return list;
    }else{
        NSLog(@"ERROR");
    }
    return list;
}
- (NSString *)filePath{
    NSString *databaseFilePath = [[NSBundle mainBundle] pathForResource:@"china_Province_city_zone" ofType:@"db"];
    return databaseFilePath;
}
- (void)openDB{
    if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK) {
        sqlite3_close(db);
        NSAssert(0, @"数据库打开失败。");
    }else{
        NSLog(@"打开数据库");
    }
}

@end
